﻿using RealTimeCharts.LineChart.common;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows;

namespace RealTimeCharts.LineChart
{
    /// <summary>
    /// 图例
    /// </summary>
    public class ColorLegend
    {
        public List<ColorLegendData> dataList = null;
        public Font font = null;

        public Pen borderPen = null;

        private Padding padding = new Padding(3, 3, 3, 3);
        public Position pos;

        public ColorLegend(List<ColorLegendData> dataList, Font f, Pen borderPen, Position pos)
        {
            this.dataList = dataList;
            this.font = f;
            this.borderPen = borderPen;
            this.pos = pos;
        }


        private Rect GetPosition(Graphics g, Rect rect, Position pos)
        {
            SizeF size = g.MeasureString("测试文字", font);
            int spaceHeight = 3;

            int width = 100;
            int height = (int)(size.Height * dataList.Count + spaceHeight * (dataList.Count - 1) + 10);

            System.Windows.Point startPoint = new System.Windows.Point(rect.Left, rect.Top);

            switch (pos)
            {
                case Position.TopRight:
                    startPoint = new System.Windows.Point(rect.Right - width, rect.Top);

                    break;

                case Position.BottomRight:
                    startPoint = new System.Windows.Point(rect.Right - width, rect.Bottom - height);

                    break;

                case Position.TopLeft:
                    startPoint = new System.Windows.Point(rect.Left, rect.Top);

                    break;

                case Position.BottomLeft:
                    startPoint = new System.Windows.Point(rect.Left, rect.Bottom - height);

                    break;
            }



            return new Rect(startPoint, new System.Windows.Size(width, height));
        }

        private System.Windows.Point GetStartPoint(Rect rect, Position pos, int width, int height)
        {

            System.Windows.Point startPoint = new System.Windows.Point(rect.Left, rect.Top);

            switch (pos)
            {
                case Position.TopRight:
                    startPoint = new System.Windows.Point(rect.Right - width, rect.Top);

                    break;

                case Position.BottomRight:
                    startPoint = new System.Windows.Point(rect.Right - width, rect.Bottom - height);

                    break;

                case Position.TopLeft:
                    startPoint = new System.Windows.Point(rect.Left, rect.Top);

                    break;

                case Position.BottomLeft:
                    startPoint = new System.Windows.Point(rect.Left, rect.Bottom - height);

                    break;
            }



            return startPoint;
        }

        public Rect GetPosition(Graphics g, Rect r)
        {
            int counts = dataList.Count;

            int textLength = 0;
            int textHeight = 0;

            for (int i = 0; i < counts; i++)
            {
                SizeF size = g.MeasureString(dataList[i].text, font);
                textLength = (int)(size.Width > textLength ? size.Width : textLength);
                textHeight = (int)size.Height;
            }

            int spaceHeight = 3;

            int borderWidth = 120;
            int borderHeight = (int)(textHeight * counts + spaceHeight * (counts - 1) + 10);


            System.Windows.Point startPoint = GetStartPoint(r, pos, borderWidth, borderHeight);

            return new Rect(startPoint.X, startPoint.Y, borderWidth, borderHeight);
        }

        public Rect GetRemainRect(Graphics g, Rect rect)
        {
            Rect myPos = GetPosition(g, rect);
            double borderWidth = myPos.Width;


            Rect rn = new Rect(rect.X, rect.Y, rect.Width - borderWidth, rect.Height);
            switch (pos)
            {
                case Position.TopRight:
                    rn = new Rect(rect.X, rect.Y, rect.Width - borderWidth, rect.Height);


                    break;

                case Position.BottomRight:
                    rn = new Rect(rect.X, rect.Y, rect.Width - borderWidth, rect.Height);

                    break;

                case Position.TopLeft:
                    rn = new Rect(rect.X + borderWidth, rect.Y, rect.Width - borderWidth, rect.Height);

                    break;

                case Position.BottomLeft:
                    rn = new Rect(rect.X + borderWidth, rect.Y, rect.Width - borderWidth, rect.Height);

                    break;
            }

            return rn;
            //return rect;

        }

        public Rect Show(Graphics g, Rect rect)
        {

            int counts = dataList.Count;

            int textLength = 0;
            int textHeight = 0;

            for (int i = 0; i < counts; i++)
            {
                SizeF size = g.MeasureString(dataList[i].text, font);
                textLength = (int)(size.Width > textLength ? size.Width : textLength);
                textHeight = (int)size.Height;
            }

            int lineHeight = 3;
            int spaceHeight = 3;

            //  int borderWidth = 120;
            //int borderHeight = (int)(textHeight * counts + spaceHeight * (counts - 1) + 10);

            // System.Windows.Point startPoint = GetStartPoint(rect, pos, borderWidth, borderHeight);


            Rect myPos = GetPosition(g, rect);
            System.Windows.Point startPoint = myPos.TopLeft;
            int borderWidth = (int)myPos.Width;
            int borderHeight = (int)myPos.Height;

            Rectangle borderRectangle = new Rectangle((int)startPoint.X, (int)startPoint.Y, (int)borderWidth, (int)borderHeight);
            g.DrawRectangle(borderPen, borderRectangle);

            int spaceWidth = 5;
            int lineLength = borderWidth - textLength - spaceWidth - padding.left - padding.right;

            if (counts > 0)
            {
                PointF p1 = new PointF((float)startPoint.X + padding.left, (float)startPoint.Y + textHeight / 2 + padding.top);
                PointF p2 = new PointF((float)p1.X + lineLength, p1.Y);
                Pen pen = new Pen(dataList[0].color, lineHeight);
                g.DrawLine(pen, p1, p2);

                PointF textPoint = new PointF(p2.X + spaceWidth, (float)startPoint.Y + padding.top);
                g.DrawString(dataList[0].text, font, new SolidBrush(pen.Color), textPoint);

                for (int i = 1; i < counts; i++)
                {
                    p1.Y += textHeight;
                    p1.Y += spaceHeight;

                    p2.Y += textHeight;
                    p2.Y += spaceHeight;

                    pen = new Pen(dataList[i].color, lineHeight);
                    g.DrawLine(pen, p1, p2);

                    textPoint.Y += textHeight;
                    textPoint.Y += spaceHeight;
                    g.DrawString(dataList[i].text, font, new SolidBrush(pen.Color), textPoint);
                }
            }

            Rect rn = GetRemainRect(g, rect);

            return rn;

        }
    }
}
